/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.utils;

import java.lang.reflect.Field;

/**
 *
 * @author Administrator
 */
public class JavaBeanUtil {

    public static Object clone(Object obj) throws Exception{
        try {
            Object newObj=obj.getClass().newInstance();
            Field[] fields = obj.getClass().getDeclaredFields();
            for(int i=0;i<fields.length;i++){
              String property=fields[i].getName();
//              fields[i].setAccessible(true);
//            fields[i].set(newObj, fields[i].get(obj));
//            fields[i].setAccessible(false);
              setValue(newObj, StringUtil.SET.concat(property), getValue(obj,  StringUtil.GET.concat(property)));
            }
            return newObj;
        } catch (Exception e) {
            throw  e;
        }
    }

   public static void setValue(Object obj, String funcName, String value) throws Exception{
        try {
            obj.getClass().getMethod(funcName, new Class[]{Class.forName("java.lang.String")}).invoke(obj, new Object[]{null==value?null:value});
        } catch (Exception e) {
            throw  e;
        }
    }

    public static String getValue(Object obj, String funcName) throws Exception {
        String value = "";
        try {
            Class[] argClasses = null;
            Object[] args = null;
            value = (String) obj.getClass().getMethod(funcName, argClasses).invoke(obj, args);
        } catch (Exception e) {
           throw  e;
        }
        return value;
    }
}